package cn.jdx.tools.md4img.tools;

import java.io.File;

public class MdToolsIsLocalFile {

    private String url; //如果是网络url那么就记录url

    //[仅仅适用于markdown语法] 判断是否是网络图片
    public boolean isUrl(String line) {
        String checkLine = line.trim();
        if (checkLine.indexOf("![") == 0) {
            if (checkLine.contains("http") && checkLine.contains("](") && checkLine.contains("/")) {
                url = getSubImgTagContent(checkLine);
                return true;
            }
        }
        return false;
    }

    //[仅仅适用于markdown文档]
    public String getSubImgTagContent(String content) {
        String checkLine = content.trim();
        //(1).第一阶段的修剪,提取"]("之后的所有数据
        String startChar = "](";
        int start = checkLine.lastIndexOf(startChar);
        String headString = checkLine.substring(start + startChar.length());
        //System.out.println(headString);
        //(2).第二阶段的修剪,消去")"字符
        String endChar = ")";
        String endString = headString.substring(0, headString.length() - endChar.length());
        //System.out.println(endString);
        return endString;
    }


    private String filePath = "";//如果是本地图片,那么就记录当前的图片的位置

    //对相对路径进行检测
    private static String getShortPath(String imgTagContent) {
        String lastPath = "";
        if (imgTagContent.contains("/") || imgTagContent.contains("\\")) {
            //[1]处理左斜杠
            if (imgTagContent.indexOf("./") == 0) {
                if (imgTagContent.indexOf(".//") == 0) {
                    lastPath = imgTagContent.replaceFirst(".//", "");
//                    System.out.println("[getShortPath]:from(" + imgTagContent + ")\t=>\tto(" + lastPath + ")");
                    return lastPath;
                }
                lastPath = imgTagContent.replaceFirst("./", "");
//                System.out.println("[getShortPath]:from(" + imgTagContent + ")\t=>\tto(" + lastPath + ")");
                return lastPath;
            }
            //[2]处理右斜杠
            if (imgTagContent.indexOf(".\\") == 0) {
                if (imgTagContent.indexOf(".\\\\") == 0) {
                    lastPath = imgTagContent.replaceFirst(".\\\\\\\\", "");
//                    System.out.println("[getShortPath]:from(" + imgTagContent + ")\t=>\tto(" + lastPath + ")");
                    return lastPath;
                }
                lastPath = imgTagContent.replaceFirst(".\\\\", "");
//                System.out.println("[getShortPath]:from(" + imgTagContent + ")\t=>\tto(" + lastPath + ")");
                return lastPath;
            }
            return imgTagContent;
        }
        //既没有左斜杠，也没有右斜杠
        return imgTagContent;
    }

    //[仅仅适用于markdown语法] 判断是否是本地图片
    public boolean isLocalFile(String line, File currentMdFile) {
        String checkLine = line.trim();
        if (checkLine.indexOf("![") == 0 && checkLine.contains("](")) {
            //[1].开始提取本地图片的地址
            String subImgTagContent = getSubImgTagContent(checkLine);
            File file = new File(subImgTagContent);
            //[2].判断文件是否存在
            if (file.exists()) {
                filePath = file.getAbsolutePath();
                return true;
            } else {
                //根据[img]标签内容，拿到相对路径的短路径
                String lastPath = getShortPath(subImgTagContent);
                this.filePath = new File(currentMdFile.getParentFile().getAbsolutePath(), lastPath).getAbsolutePath();
//                System.out.println("[isLocalFile]:filePath="+this.filePath);
                return true;
            }
        }
        return false;
    }


    public String getUrl() {
        return url;
    }

    public String getFilePath() {
        return filePath;
    }
}
